﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace NCKH.Admin
{
    public partial class Nganhdaotao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                hienNDT();
        }
        private void hienNDT()
        {
            using (DataTable tbl = getNDT())
            {
                grvNDT.DataSource = tbl;
                grvNDT.DataBind();
            }
        }
        private DataTable getNDT()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlSelect = "SELECT MaNganh,Tennganh"
                + " FROM NGANHDAOTAO"
                + " ORDER BY Tennganh ASC";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }
        protected void grvNDT_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int nhaxuatbanID = Convert.ToInt32(e.CommandArgument);
                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection Cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand Cmd = new SqlCommand("Nganhdaotao_Xoa", Cnn))
                    {
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters.AddWithValue("MaNganh", nhaxuatbanID);
                        Cnn.Open();
                        Cmd.ExecuteNonQuery();
                        Cnn.Close();
                        hienNDT();
                    }
                }
            }
        }
        protected bool NDT_Check()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlCheck = "SELECT MaNganh, Tennganh FROM NGANHDAOTAO WHERE Tennganh like N'" + txtNDT.Text + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlCheck, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            lblNDT.Text = "Tên ngành đào tạo đã tồn tại";
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }

        }
        protected void ThemmoiNDT()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Nganhdaotao_Insert", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Tennganh", txtNDT.Text);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void btnThem_Click(object sender, EventArgs e)
        {
            //if (NDT_Check() == false)
            ThemmoiNDT();
            hienNDT();
            Boqua();
        }
        protected void Boqua()
        {
            txtNDT.Text = String.Empty;
            txtNDT.Focus();
        }
        protected void btnBoqua_Click(object sender, EventArgs e)
        {
            Boqua();
        }

        protected void grvNDT_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvNDT.EditIndex = e.NewEditIndex;
            hienNDT();
        }
        protected void grvDNT_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int catID = int.Parse(grvNDT.DataKeys[e.RowIndex].Value.ToString());
            string strcatname = ((TextBox)grvNDT.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlNDT_Update = "UPDATE NGANHDAOTAO SET Tennganh= N'" + strcatname + "' WHERE MaNganh=" + catID;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                SqlDataAdapter da = new SqlDataAdapter("", Cnn);
                Cnn.Open();
                da.UpdateCommand = new SqlCommand(sqlNDT_Update, Cnn);
                da.UpdateCommand.ExecuteNonQuery();
                Cnn.Close();
                grvNDT.EditIndex = -1;
                hienNDT();

            }
        }
        protected void grvNDT_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvNDT.EditIndex = -1;
            hienNDT();
        }
    }
}